<?php
class TopItemController  extends Controller {

	public function __construct($data) {
        parent::__construct($data);
    }
	
public function index() {
	    require_once( '../system/ecjp_m.php' );
	    $this->_load_php_file("chklogin.php");
        if(!isset($_SESSION['USER'])){	    	
	    require_once( '../system/cookie.php' );  
	    }
	    $this->_load_php_file("Class_FUNC.php");
	    $this->_load_mdao_class("t_m_goods");
	    $goods_dao = new t_m_goods;
	    
	    
	    
         if ( isset($_SESSION['USER']) ){
	       	   $user = $_SESSION['USER'];
	       	   $login_id = $user['USER_id'];
	       	   $db = new Class_M_DAO;
    		   $sql = sprintf("SELECT * FROM D_HOPE WHERE login_id = '%s' AND del<>'*' ",
			   mysql_real_escape_string( $login_id ));
    		   $data = $db->get_rows( $sql );
	       	   
	       	    if (isset($user))
	            $this->assign('user', $user);
	       }
	    
	    
	    
	    
	    //商品詳細
	    $g = $this->g;
        if(isset ( $g ['id'] ) && $g ['id'] != '') {
				if (is_numeric( $g ['id'] )) {
					$sql="SELECT * FROM d_goods a LEFT JOIN m_categorygood b ON a.category_id = b.category_cd ";
					$sql.=" where a.id='".$g ['id']."'";
					$forms = $goods_dao->get_rows($sql);				
				}
			} 
	   
		
           if(isset($data) && !empty($data)){
        	   foreach ($data as $value) {
                 if(isset($value['goods_id']) && (!empty($value['goods_id']))){
    				 $id[] = $value['goods_id']; 				 
    			}                        
              }
              
            if(in_array($forms[0]['id'],$id)){
			 	$forms[0]['Y'] = 'Y';
             }
           }
		
       if (isset ($forms[0])){
                           	
        $forms[0]['goods_image'] = Class_FUNC::get_imagepath($forms[0]['id'],$forms[0]['goods_image'],102);
         $forms[0]['goods_rate'] = $forms[0]['goods_unitprice']+$forms[0]['goods_unitprice'] * RATE;
		$this->assign ( 'forms', $forms[0]);
        }
           
           
           
		//商品コメント
		if(isset ( $g ['id'] ) && $g ['id'] != '') {
				if (is_numeric( $g ['id'] )) {
					$sql="SELECT * FROM d_goodsapp a
                          LEFT JOIN (
                          SELECT c.login_id, c.user_name1, c.user_name2, c.sex, d.local_name
                          FROM m_user c
                          LEFT JOIN m_local d ON c.local_cd = d.local_cd
                          )b ON a.login_id = b.login_id";
					$sql.=" WHERE a.goods_id ='".$g ['id']."'";
					$goodsapp = $goods_dao->get_rows($sql);				
				}
			} 
	    if (isset ( $goodsapp ))
		$this->assign ( 'goodsapp', $goodsapp);
	    
	     
		//売上ランキング
	     $sql = "SELECT * FROM `d_goods` WHERE  goods_mark1='1' order by sellbegin desc LIMIT 0,10";
	     $charts_data = $goods_dao->get_rows($sql);
	      foreach ($charts_data as &$value){
	          if(isset($value['goods_image']) && !empty($value['goods_image'])){
	     	     $value['goods_image'] = Class_FUNC::get_imagepath($value['id'],$value['goods_image'],101);
	       }
	       if(isset($value['goods_unitprice']) && !empty($value['goods_unitprice'])){
	          	 $value['goods_rate'] = $value['goods_unitprice']+$value['goods_unitprice'] * RATE;
	          }
	     }
	     if (isset($charts_data))
	     $this->assign('charts_data',$charts_data);
	    
	      //商品category
	       $sql = 'SELECT * FROM  m_categorygood';
           $category_data = $goods_dao->get_rows($sql);
           if (isset($category_data))
           $this->assign('category_data', $category_data);
         //What's New
             $this->_load_mdao_class("t_m_news");
	        $news_dao = new t_m_news;
           $sql = 'SELECT * FROM  m_news order by date desc limit 0,10';
           $newslist_data = $news_dao->get_rows($sql);
           if (isset($newslist_data))
           $this->assign('news_datalist', $newslist_data);
           
           
         $this->assign('categorying', $this->fetch('sp/categorying.html'));
	     $this->assign('newsing', $this->fetch('sp/news.html'));  
	     $this->assign('sp_head', $this->fetch('sp/sp_head.html'));
	     $this->assign('chartsing', $this->fetch('sp/charts.html'));
	     $this->assign('sp_foot', $this->fetch('sp/sp_foot.html'));
	     $this->display('sp/topitem.html');
	}
	
	
 public  function add(){
    	     require_once( '../system/ecjp_m.php' );
     	         $this->_load_php_file("Class_DB.php");
     	         $p=$this->p;
     	         
     	         $good_id = $p['goods_id'];
     	         $login_id = $p['login_id'];
     	         $sql = sprintf("INSERT INTO D_HOPE(login_id,goods_id) VALUES ('%s','%s') ",
			     mysql_real_escape_string( $login_id ),
			     mysql_real_escape_string( $good_id ));
     	         
     	         $db = new Class_DB;
     	         $db->query( $sql );   
    	
    }
    
}


?>
